/**
 * Copyright (c) 2015-2020, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jboot.components.rpc;


import io.jboot.Jboot;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public abstract class JbootrpcBase implements Jbootrpc {

    protected static final Map<String, Object> objectCache = new ConcurrentHashMap<>();
    protected static JbootrpcConfig rpcConfig = Jboot.config(JbootrpcConfig.class);
    private boolean started = false;


    @Override
    public <T> T serviceObtain(Class<T> interfaceClass, JbootrpcReferenceConfig config) {

        String key = buildCacheKey(interfaceClass, config);
        T object = (T) objectCache.get(key);
        if (object == null) {
            synchronized (this) {
                if (objectCache.get(key) == null) {
                    startIfNecessary();
                    object = onServiceCreate(interfaceClass, config);
                    if (object != null) {
                        objectCache.put(key, object);
                    }
                }
            }
        }
        return object;
    }

    protected void startIfNecessary() {
        if (!started) {
            synchronized (this) {
                if (!started) {
                    onStart();
                }
            }
        }
    }

    public abstract <T> T onServiceCreate(Class<T> serviceClass, JbootrpcReferenceConfig config);

    @Override
    public void onStart() {
        setStarted(true);
    }


    @Override
    public void onStop() {

    }

    protected String buildCacheKey(Class interfaceClass, JbootrpcReferenceConfig config) {
        StringBuilder sb = new StringBuilder(interfaceClass.getName());
        return sb.append(":").append(config.getGroup())
                .append(":").append(config.getVersion())
                .toString();
    }

    public boolean isStarted() {
        return started;
    }

    public void setStarted(boolean started) {
        this.started = started;
    }
}
